﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.Data;

using DocumentFormat.OpenXml.Packaging;

namespace ExcelSax.UT {
    [TestFixture]
    public class ConnectionUT {
        private String _xlsxFileName;
        private String _sharedStringsLoadingFileName;
        private ExcelSax.UT.RessourceLoader _l;
        
        // ====================================================================
        [SetUp]
        public void Init() {            
            _l = new ExcelSax.UT.RessourceLoader(ExcelSax.UT.RessourceLoader.ExcelSaxUTAssemblyName);

            _xlsxFileName = "abc.xlsx";
            _l.GetRessourceToFile("ExcelSax.UT.Ressources.tests.xlsx", _xlsxFileName);
            _sharedStringsLoadingFileName = "sharedStringsLoading.xlsx";
            _l.GetRessourceToFile("ExcelSax.UT.Ressources.SharedStringsLoading.xlsx", _sharedStringsLoadingFileName);
        }
        // ====================================================================
        [Test]
        public void Connection_Is_Created_Closed() {
            String utXlsxFile = _l.DuplicateFile(_xlsxFileName);
            ExcelSaxConnection esc = new ExcelSaxConnection(utXlsxFile);

            Assert.That(esc.State, Is.EqualTo(ConnectionState.Closed));

            System.IO.File.Delete(utXlsxFile);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Open_On_Null_File_Name_File_Throw_ArgumentNull_Exception() {
            ExcelSaxConnection esc = new ExcelSaxConnection(null);

            Assert.That(() => esc.Open(), Throws.TypeOf<System.ArgumentNullException>());
        }
        // --------------------------------------------------------------------
        [Test]
        public void Open_On_Non_OpenXml_File_Name_File_Throw_FileFormatException_Exception() {
            String _nonXlsxFileName = System.Guid.NewGuid().ToString();
            System.IO.StreamWriter sw = new System.IO.StreamWriter(_nonXlsxFileName);
            sw.Close();

            ExcelSaxConnection esc = new ExcelSaxConnection(_nonXlsxFileName);

            Assert.That(() => esc.Open(), Throws.TypeOf<System.IO.FileFormatException>());

            esc.Close();
            System.IO.File.Delete(_nonXlsxFileName);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Open_On_Empty_File_Name_File_Throw_FileNotFoundException_Exception() {
            ExcelSaxConnection esc = new ExcelSaxConnection(String.Empty);

            Assert.That(() => esc.Open(), Throws.TypeOf<System.IO.FileNotFoundException>());           
        }
        // --------------------------------------------------------------------
        [Test]
        public void Open_On_Non_Existing_File_Throw_FileNotFoundException_Exception() {
            ExcelSaxConnection esc = new ExcelSaxConnection("non_existing_file.xlsx");

            Assert.That(() => esc.Open(), Throws.TypeOf<System.IO.FileNotFoundException>());
        }
        // --------------------------------------------------------------------
        [Test]
        public void Open_Leave_Connection_State_To_Open() {
            String utXlsxFile = _l.DuplicateFile(_xlsxFileName);

            using ( ExcelSaxConnection esc = new ExcelSaxConnection(utXlsxFile) ) {
                esc.Open();
                Assert.That(esc.State, Is.EqualTo(ConnectionState.Open));
            }

            System.IO.File.Delete(utXlsxFile);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Can_Load_The_Good_Number_Of_Sheet_In_Sheets() {
            String utXlsxFile = _l.DuplicateFile(_xlsxFileName);
            ExcelSaxConnection esc = new ExcelSaxConnection(utXlsxFile);

            Assert.That(esc.Sheets.Count, Is.EqualTo(3));

            esc.Close();

            System.IO.File.Delete(utXlsxFile);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Can_Load_All_Sheets_With_Good_Name() {
            String utXlsxFile = _l.DuplicateFile(_xlsxFileName);

            using (ExcelSaxConnection esc = new ExcelSaxConnection(utXlsxFile)) {
                esc.Open();
                Assert.That((from s in esc.Sheets where s.Name == "F1" select s).Count() == 1, Is.True);
                Assert.That((from s in esc.Sheets where s.Name == "F2" select s).Count() == 1, Is.True);
                Assert.That((from s in esc.Sheets where s.Name == "F3" select s).Count() == 1, Is.True);
            }

            System.IO.File.Delete(utXlsxFile);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Can_Load_Good_Number_Of_String_In_SharedStrings() {
            String utXlsxFile = _l.DuplicateFile(_sharedStringsLoadingFileName);

            using (ExcelSaxConnection esc = new ExcelSaxConnection(utXlsxFile)) {
                esc.Open();
                Assert.That(esc.SharedStringTableAsList.Count, Is.EqualTo(4));
            }

            System.IO.File.Delete(utXlsxFile);
        }
        // --------------------------------------------------------------------
        [Test]
        public void Can_Load_SharedStrings() {
            String utXlsxFile = _l.DuplicateFile(_sharedStringsLoadingFileName);

            using (ExcelSaxConnection esc = new ExcelSaxConnection(utXlsxFile)) {
                esc.Open();
                Assert.That((from s in esc.SharedStringTableAsList where s == "ss I" select s).Count() == 1, Is.True);
                Assert.That((from s in esc.SharedStringTableAsList where s == "ss II" select s).Count() == 1, Is.True);
                Assert.That((from s in esc.SharedStringTableAsList where s == "ss III" select s).Count() == 1, Is.True);
                Assert.That((from s in esc.SharedStringTableAsList where s == "ss IV" select s).Count() == 1, Is.True);
            }

            System.IO.File.Delete(utXlsxFile);
        }
    }
}
